package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.constant.SystemConstants;
import org.summer.lawyer.domain.bo.wxuser.WxUserBo;
import org.summer.lawyer.domain.vo.wxuser.ChatUserVo;
import org.summer.lawyer.domain.vo.wxuser.CustomerServiceVo;
import org.summer.lawyer.domain.vo.wxuser.WxUserVo;
import org.summer.lawyer.service.ISpuOrderService;
import org.summer.lawyer.service.IWxUserService;
import org.summer.system.domain.vo.SysUserVo;
import org.summer.system.service.ISysUserService;

import java.util.List;

/**
 * 微信用户信息
 *
 * @author summer
 * @date 2024-06-16
 */
@Tag(name = "后台相关接口", description = "后台微信用户信息相关接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/wxUser")
public class WxUserController extends BaseController {

    private final IWxUserService wxUserService;

    private final ISpuOrderService spuOrderService;

    private final ISysUserService sysUserService;

    /**
     * 查询微信用户信息列表
     */
    @Operation(summary = "查询微信用户信息列表", description = "查询微信用户信息列表", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("wxuser:wxuser:list")
    @GetMapping("/list")
    public TableDataInfo<WxUserVo> list(WxUserVo vo, PageQuery pageQuery) {
        vo.setWxUserType(SystemConstants.WX_USER_TYPE_NORMAL);
        return wxUserService.selectPageList(vo, pageQuery);
    }

    /**
     * 获取微信用户信息详细信息
     *
     * @param wxUserId 主键
     */
    @Operation(summary = "查询微信用户信息列表", description = "查询微信用户信息列表", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("wxuser:wxuser:query")
    @GetMapping("/{wxUserId}")
    public R<WxUserVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable Long wxUserId) {
        return R.ok(wxUserService.selectWxUserVoById(wxUserId));
    }

    /**
     * 删除微信用户信息
     *
     * @param wxUserIds 主键串
     */
    @Operation(summary = "注销微信用户信息", description = "注销微信用户信息", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("wxuser:wxuser:remove")
    @Log(title = "微信用户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wxUserIds}")
    public R<Void> removeWxUser(@NotEmpty(message = "主键不能为空")
                                @PathVariable Long[] wxUserIds) {
        return toAjax(wxUserService.deleteWithValidByIds(List.of(wxUserIds), true));
    }

    @Operation(description = "重置密码")
    @SaCheckPermission("wxuser:wxuser:resetpwd")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPassword")
    public R<Void> resetPassword(@Validated @RequestBody WxUserVo vo) {
        wxUserService.resetPassword(vo);
        return R.ok();
    }

    @Operation(description = "获取客服电话信息")
    @SaCheckLogin
    @GetMapping("/getSupport")
    public R<CustomerServiceVo> getSupport() {
        return R.ok(wxUserService.selectCustomerServiceVo());
    }

    @Operation(description = "保存客服信息")
    @SaCheckPermission("wxuser:wxuser:support")
    @PostMapping("/saveSupport")
    public R<Void> saveSupport(@Validated @RequestBody CustomerServiceVo vo) {
        wxUserService.saveCustomerService(vo);
        return R.ok();
    }

    @Operation(description = "查询在线客服列表")
    @SaCheckLogin
    @GetMapping("/listChatUser")
    public R<List<ChatUserVo>> listChatUser() {
        return R.ok(wxUserService.selectListChatUser());
    }

    @Operation(description = "查询所有系统用户列表")
    @SaCheckLogin
    @GetMapping("/listAllChatUser")
    public R<List<ChatUserVo>> listAllChatUser() {
        List<ChatUserVo> chatUserVos = CollUtil.newArrayList();
        List<SysUserVo> userVoList = sysUserService.selectUserListAll();
        List<ChatUserVo> checkedChatUserVos = wxUserService.selectListChatUser();
        if (CollUtil.isNotEmpty(userVoList)) {
            for (SysUserVo sysUserVo : userVoList) {
                ChatUserVo chatUserVo = new ChatUserVo();
                chatUserVo.setChecked(false);
                if(checkedChatUserVos.stream().anyMatch(item -> item.getChatId().equals(String.valueOf(sysUserVo.getUserId())))) {
                    chatUserVo.setChecked(true);
                }
                chatUserVo.setChatId(String.valueOf(sysUserVo.getUserId()));
                chatUserVo.setChatName(sysUserVo.getNickName());
                chatUserVos.add(chatUserVo);
            }
        }
        return R.ok(chatUserVos);
    }

    @Operation(summary = "冻结用户")
    @SaCheckPermission("wxuser:wxuser:lock")
    @PostMapping("/lockedUser")
    public R<Void> lockedUser(@Validated @RequestBody WxUserVo vo) {
        vo.setStatus("1");
        wxUserService.changeWxUserStatus(vo);
        return R.ok();
    }

    @Operation(summary = "解冻用户")
    @SaCheckPermission("wxuser:wxuser:unlock")
    @PostMapping("/unlockUser")
    public R<Void> unlockedUser(@Validated @RequestBody WxUserVo vo) {
        vo.setStatus("0");
        wxUserService.changeWxUserStatus(vo);
        return R.ok();
    }

    @Operation(summary = "设置标签")
    @SaCheckPermission("wxuser:wxuser:tag")
    @PostMapping("/saveTag")
    public R<Void> saveTag(@Validated @RequestBody WxUserBo bo) {
        wxUserService.updateWxUserTag(bo);
        return R.ok();
    }


}
